package com.lyp.qczp.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.lyp.qczp.dao.Employeedao;
import com.lyp.qczp.dao.Mianshidao;
import com.lyp.qczp.entity.Employee;
import com.lyp.qczp.entity.Mianshi;

@Service
public class MianshiServiceImp implements MianshiService{

	@Autowired
	private Mianshidao Mianshidao;
	@Autowired
	private Employeedao employeedao;
	
	@Override
	public int save(Mianshi mianshi) {
		// TODO Auto-generated method stub
		Mianshi save = Mianshidao.save(mianshi);
		if (save!=null) {
			return 1;
		}
		return 0;
	}

	@Override
	public int deleteById(Integer eId) {
		Mianshidao.deleteById(eId);
		return 1;
	}

	@Override
	public Map<String, Object> findAll(PageRequest pageable,Mianshi mianshi) {
		Page<Mianshi> findAll = Mianshidao.findAll(selectquanpage(mianshi),pageable);
		
		Map<String, Object> map = new HashMap<String, Object>();
		List<Mianshi> content = findAll.getContent();
		for (int i = 0; i < content.size(); i++) {
			
			Employee findByeId = employeedao.findByeId(content.get(i).getMsEid());
			System.out.println(findByeId);
			content.get(i).setEmployee(findByeId);
		}
		System.out.println(content);
		map.put("rows", content);
		map.put("total", findAll.getTotalElements());
		return map;
	}


	//条件查询Specification
		private Specification<Mianshi> selectquanpage(Mianshi mianshi){
			return new Specification<Mianshi>() {

				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				@Override
				public Predicate toPredicate(Root<Mianshi> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
					// TODO Auto-generated method stub
					Predicate conjunction = cb.conjunction();
	//				List<Expression<Boolean>> expressions = conjunction.getExpressions();
//					if (employee.getEYongchu()!=null&&employee.getEYongchu()!=0) {
//						expressions.add(cb.equal(root.<Integer>get("eYongchu"), employee.getEYongchu()));
//					}
					
					
					return conjunction;
				}
			};
		}
	
	
	
	
	
}
